/**
 * @file btn_drv.cpp
 * @author LinTeX9527 (lintex9527@yeah.net)
 * @brief
 * @version 0.1
 * @date 2024-11-20
 *
 * @copyright Copyright (c) 2024
 *
 */

#include <Arduino.h>
#include <Bounce2.h>
#include "btn_drv.h"
#include "led_manager.h"

/* Constants -------------------------------------------------------------------------------------*/

/* Data Types ------------------------------------------------------------------------------------*/

/* Variables -------------------------------------------------------------------------------------*/

Bounce btnBootObj = Bounce(BTN_BOOT_PIN, 10);
Bounce btnRedObj  = Bounce(BTN_RED_PIN, 10);
Bounce btnBlueObj = Bounce(BTN_BLUE_PIN, 10);


/**
 * @brief 按键硬件初始化、对象初始化
 */
void setup_btn(void)
{
	/* BOOT 按键 */
	pinMode(BTN_BOOT_PIN, INPUT_PULLUP); /* 按键硬件初始化，输入上拉模式 */

	/* 红色按键 */
	pinMode(BTN_RED_PIN, INPUT_PULLUP);

	/* 蓝色按键 */
	pinMode(BTN_BLUE_PIN, INPUT_PULLUP);
}

/**
 * @brief 判断当前按键是否按下
 * id 0 是 BOOT 按键ID
 * id 1 是 RED 按键 ID
 * id 2 是 BLUE 按键 ID
 * @param id
 * @return true 按键按下
 * @return false 按键没有按下
 */
bool btn_is_pressed(uint8_t id)
{
	if (BTN_ID_0_BOOT == id) { // BOOT - 翻页
		btnBootObj.update();
		if (btnBootObj.fell()) {
			Serial.println(String(__FUNCTION__) + " ---> BOOT");
			return true;
		}
	}

	if (BTN_ID_1_RED == id) {
		btnRedObj.update();
		if (btnRedObj.fell()) {
			Serial.println(String(__FUNCTION__) + " ---> RED");
			return true;
		}
	}

	if (BTN_ID_2_BLUE == id) {
		btnBlueObj.update();
		if (btnBlueObj.fell()) {
			Serial.println(String(__FUNCTION__) + " ---> BLUE");
			return true;
		}
	}

	return false;
}
